// src/api/student.ts
import request from '@/utils/request'
import service from './service'

// 获取课题列表
export const getProjects = (params?: {
  page?: number
  size?: number
  keyword?: string
  category?: string
  status?: string
  budget_min?: number
  budget_max?: number
  is_public?: string
}): Promise<{
  code: number
  message: string
  data: Project[]
  total: number
  page: number
  size: number
  pages: number
  trace_id: string
}> => {
  return request({
    url: '/projects',
    method: 'get',
    params
  })
}

// 获取匹配课题列表
export const getMyMatches = (params?: {
  id?: number
  page?: number
  size?: number
}): Promise<{
  code: number
  message: string
  data: Project[]
  total: number
  page: number
  size: number
  pages: number
  trace_id: string
}> => {
  return request({
    url: '/matches/my',
    method: 'get',
    params
  })
}

// 搜索课题
export const searchProjects = (params: {
  q: string
  page?: number
  size?: number
  category?: string
  budget_min?: number
  budget_max?: number
}): Promise<{
  code: number
  message: string
  data: Project[]
  total: number
  page: number
  size: number
  pages: number
  trace_id: string
}> => {
  return request({
    url: '/projects/search',
    method: 'get',
    params
  })
}

// 获取课题分类
export const getProjectCategories = (): Promise<{
  code: number
  message: string
  data: {
    name: string
    count: number
    avg_budget: number
  }[]
  trace_id?: string
}> => {
  return request({
    url: '/api/v1/projects/categories',
    method: 'get'
  })
}

// 获取课题详情
export const getProjectDetail = (id: number): Promise<{
  code: number
  message: string
  data: Project
  trace_id: string
}> => {
  return request({
    url: `/projects/${id}`,
    method: 'get'
  })
}

// 申请课题
export const applyProject = (id: number, data: {
  cover_letter: string
  portfolio?: string
  resume?: string
}): Promise<any> => {
  return service.post(`/projects/${id}/applications`, data)
}

// 获取我的申请列表
export const getMyApplications = (params?: {
  page?: number
  size?: number
}) => {
  return service.get('/applications/my', { params })
}

// 获取申请列表（保留原接口用于其他场景）
export const getApplications = (params?: {
  page?: number
  size?: number
  status?: string
}) => {
  return service.get('/applications', { params })
}

// 获取申请详情
export const getApplicationDetail = (id: number) => {
  return service.get(`/applications/${id}`)
}

// 创建申请
export const createApplication = (data: {
  project_id: number
  cover_letter: string
  portfolio: string
  resume: string
  motivation?: string
  qualifications?: string
  research_plan?: string
  expected_time?: number
  start_date?: string
  end_date?: string
}) => {
  return service.post('/applications', data)
}

// 更新申请状态
export const updateApplicationStatus = (id: number, status: 'pending' | 'accepted' | 'rejected' | 'withdrawn') => {
  return service.put(`/applications/${id}/status`, { status })
}

// 撤回申请
export const withdrawApplication = (id: number): Promise<{
  code: number
  message: string
  data: null
  trace_id: string
}> => {
  return service.delete(`/applications/${id}`)
}

// 获取我的项目
export const getMyProjects = (params?: {
  page?: number
  size?: number
}): Promise<{
  code: number
  message: string
  data: MyProject[]
  total: number
  page: number
  size: number
  pages: number
  trace_id: string
}> => {
  return service.get('/projects/my', { params })
}

// 获取学习资源
export const getResources = (params?: {
  page?: number
  size?: number
  keyword?: string
}): Promise<{
  data: Resource[]
  total: number
}> => {
  return request({
    url: '/resources',
    method: 'get',
    params
  })
}

// 获取通知列表
export const getNotifications = (params?: {
  page?: number
  size?: number
}) => {
  return service.get('/notifications', { params })
}

// 获取未读通知数量
export const getUnreadNotificationCount = () => {
  return service.get('/notifications/unread/count')
}

// 标记通知为已读
export const markNotificationAsRead = (id: number) => {
  return service.put(`/notifications/${id}/read`)
}

// src/types/student.d.ts
export interface Project {
  id: number
  created_at: string
  updated_at: string
  title: string
  description: string
  requirements: string                    // 接口返回字符串格式
  budget: number
  deadline: string
  category: string
  tags: string                           // 接口返回字符串格式
  status: 'open' | 'closed' | 'in_progress' | 'completed'
  contact_info?: string
  is_public: boolean
  view_count: number
  apply_count: number
  priority: number
  location: string
  skills: string
  duration: number
  max_applicants: number
  current_applicants: number
  start_date?: string | null
  end_date?: string | null
  creator_id: number
  creator?: {
    id: number
    username: string
    email: string
    role: string
    company?: string
    created_at: string
  }
  applications: Application[]
  matched_users: any[]
  // 前端计算字段
  enterprise?: string
  statusText?: string
  hasApplied?: boolean
  publishTime?: string
  content?: string
  expectedOutcome?: string
}

export interface Application {
  id: number
  project_id: number
  applicant_id: number
  status: 'pending' | 'accepted' | 'rejected' | 'withdrawn'
  cover_letter: string
  resume: string
  portfolio: string
  apply_date: string
  applicant?: {
    id: number
    username: string
    email: string
    role: string
  }
  // 前端计算字段
  topicTitle?: string
  enterprise?: string
  applyTime?: string
  statusText?: string
  feedback?: string
  research_plan?: string
  motivation?: string
  qualifications?: string
  expected_time?: number
  start_date?: string
  end_date?: string
}

export interface MyProject {
  id: number
  title: string
  description: string
  enterprise: string
  mentor: string
  startDate: string
  progress: number
  status: 'ongoing' | 'completed' | 'pending' | 'problem'
  statusText: string
}

export interface Resource {
  id: number
  title: string
  description: string
  type: 'tutorial' | 'guide' | 'course' | 'document'
  typeText: string
  author: string
  date: string
}

export interface Notification {
  id: number
  title: string
  content: string
  isRead: boolean
  createdAt: string
}

